<template>
  <div>
    <a-card>
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-model-item prop="key" label="流程KEY">
                <a-input v-model="queryParam.key" allowClear placeholder="请输入流程KEY" ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button @click="searchQuery" type="primary" icon="search" style="margin-right: 8px">查询</a-button>
                <a-button @click="openModeler" type="primary" icon="vertical-align-bottom">导入流程</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        :columns="columns"
        :dataSource="dataSource"
        rowKey="id"
        :loading="loading"
        class="j-table-force-nowrap"
        :pagination="ipagination"
        @change="handleTableChange"
      >
        <template slot="suspensionState" slot-scope="text,record">
          <span v-if="record.suspensionState =='1' " style="color: #7ac23c">激活</span>
          <span v-if="record.suspensionState =='2' " style="color: #ff0000">挂起</span>
        </template>

        <span slot="action" slot-scope="text, record">

          <a v-if="record.suspensionState =='1' " style="color: #ff0000"@click="updateHandle(record.id,2)">挂起</a>
          <a v-else style="color: #7ac23c" @click="updateHandle(record.id,1)">激活</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="convertToModel(record.id)">转为模型</a>
              </a-menu-item>
              <a-menu-item v-if="record.suspensionState =='2' ">
                <a-popconfirm title="确定删除吗?" @confirm="() => actDelete(record.deploymentId)">
                  <a style="color: red">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <a-divider type="vertical" />
      </a-table>

      <activiti-file-model ref="ativitiFileModel" ></activiti-file-model>
    </a-card>
  </div>

</template>

<script>

  import { getAction, deleteAction } from '@/api/manage'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ActivitiFileModel from './ActivitiFileModel'
  import { getModelList } from '@/api/activiti/activiti'

  export default {
    name: 'ActivitiFileList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      ActivitiFileModel
    },
    data() {
      return {
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            }
          }, {
            dataIndex: 'deploymentId',
            title: '流程编号',
            align: 'center'
          }, {
            dataIndex: 'name',
            title: '流程名称',
            align: 'center'
          }, {
            dataIndex: 'key',
            title: '流程KEY',
            align: 'center'
          }, {
            dataIndex: 'description',
            title: '描述信息',
            align: 'center'
          }, {
            title: '是否挂起',
            dataIndex: 'suspensionState',
            align: 'center',
             scopedSlots: { customRender: 'suspensionState' }
          }, {
            title: '部署时间',
            dataIndex: 'deployTime',
            align: 'center'
          }, {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }],
        url: {
          list: 'act/reprocdef/list',
          delete: '/act/reprocdef/deleteByDeploymentId',
          convertToModel: '/act/reprocdef/convertToModel',
          updateHandle: '/act/reprocdef/update'
        },
        queryParam: {
          category: '',
          key: '',
          name: ''
        }
       }
    },
   created() {
     // this.getDataList()
   },
   methods: {
     // 删除
     actDelete(deploymentId) {
        deleteAction(this.url.delete, { deploymentId: deploymentId }).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.searchQuery()
            } else {
              this.$message.warning(res.message)
            }
        })
     },
     // 转为模型
     convertToModel(id) {
       getAction(this.url.convertToModel, { id: id }).then(res => {
         console.log('转为模型', res)
        if (res.success) {
          this.$message.success('操作成功，流程编号:' + res.result.deploymentId)
           this.searchQuery()
        } else {
          this.$message.warning('操作失败，流程编号:' + res.result.deploymentId)
        }
       })
     },
     // 挂起和激活
     updateHandle(id, state) {
       getAction(this.url.updateHandle, { id: id, state: state }).then(res => {
         console.log('挂起和激活', res)
         if (res.success) {
           this.$message.success(res.message)
            this.searchQuery()
         } else {
           this.$message.warning(res.message)
         }
       })
     },
      // 查询
      getDataList() {
       this.loading = true
        let params = this.queryParam
        getModelList(this.url.list, params).then(res => {
          console.log('查询数据', res)
          this.dataSource = res.result.records
          this.total = res.result.total
          this.ipagination.total = res.result.total
          this.loading = false
        })
      },
     openModeler() {
          this.$refs.ativitiFileModel.openModel()
       },

      deployProcess(id) {
        getAction(this.url.deploy, { id: id }).then(res => {
          if (res.success) {
            this.$message.success(res.message)
             this.searchQuery()
          } else {
            this.$message.error(res.message)
          }
        })
      }
    }
   }
</script>

<style lang="less" scoped>
 .table-operator .ant-btn {
        margin: 3px 8px 4px 0;
    }
</style>
